package StacksAndQueues;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Bjarke
 * Date: 06-11-13
 * Time: 20:54
 * To change this template use File | Settings | File Templates.
 */
public class TheQueue {
    private String[] queueArray;
    private int queueSize;
    private int front, rear, numberOfItems = 0;

    TheQueue(int size){
        queueSize = size;
        queueArray = new String[size];
        Arrays.fill(queueArray, "-1");
    }

    public void insert(String input){
        if ( numberOfItems +1 <= queueSize ){
            queueArray[rear] = input;
            rear++;
            numberOfItems++;
            System.out.println("Insert " + input + " was added into the Queue\n");
        } else System.out.println("Sorry but the queue is full");
    }

    public void removeItems(){
        if (numberOfItems > 0){
            System.out.println("Remove " + queueArray[front] + " was removed from the Queue\n");
            queueArray[front] = "-1";
            front++;
            numberOfItems--;
        } else System.out.println("Sorry but the queue is empty");
    }

    public void peek(){
        System.out.println("Front of the queue " + queueArray[front]);
    }


    public void displayTheQueue(){

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < queueSize; n++){

            System.out.format("| %2s "+ " ", n);

        }

        System.out.println("|");

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

        for(int n = 0; n < queueSize; n++){


            if(queueArray[n].equals("-1")) System.out.print("|     ");

            else System.out.print(String.format("| %2s "+ " ", queueArray[n]));

        }

        System.out.println("|");

        for(int n = 0; n < 61; n++)System.out.print("-");

        System.out.println();

        // Number of spaces to put before the F

        int spacesBeforeFront = 3*(2*(front+1)-1);

        for(int k = 1; k < spacesBeforeFront; k++)System.out.print(" ");

        System.out.print("F");

        // Number of spaces to put before the R

        int spacesBeforeRear = (2*(3*rear)-1) - (spacesBeforeFront);

        for(int l = 0; l < spacesBeforeRear; l++)System.out.print(" ");

        System.out.print("R");

        System.out.println("\n");

    }

    public void priorityInsert(String input){
        int i;
        if (numberOfItems == 0){
            insert(input);
        } else{
            for (i = numberOfItems-1; i >= 0; i--){
                if (Integer.parseInt(input) > Integer.parseInt(queueArray[i])){
                    queueArray[i+1] = queueArray[i];
                } else break;

            }

            queueArray[i+1] = input;
            rear++;
            numberOfItems++;
        }
    }

    public static void main(String[] args){
        TheQueue theQueue = new TheQueue(10);

        theQueue.priorityInsert("10");
        theQueue.priorityInsert("15");
        theQueue.priorityInsert("11");

        theQueue.displayTheQueue();

        theQueue.removeItems();
        theQueue.peek();

        theQueue.displayTheQueue();

    }
}
